package com.swinw.asumall.search.controller;

import com.swinw.asumall.search.service.ElasticsearchSaveService;
import com.swinw.common.to.SkuEsModel;
import com.swinw.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/search/save")
public class ElasticsearchSaveController {

    @Autowired
    ElasticsearchSaveService elasticsearchSaveService;

    @RequestMapping("/product")
    public R saveSkuToEsAndUp(@RequestBody List<SkuEsModel> skuEsModels){
        boolean res = false;
        try {
            res = elasticsearchSaveService.saveSkuToEs(skuEsModels);
        } catch (IOException e) {
            log.error("ElasticsearchSaveController ： 商品上上架失败");
            return R.error(0,"商品上架失败");
        }
        return res ? R.ok() : R.error(0,"商品上架失败");
    }
}
